﻿'Copyright (c) 2009, Dario Rossa
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification,
'are permitted provided that the following conditions are met:
'    * Redistributions of source code must retain the above copyright notice,
'      this list of conditions and the following disclaimer.
'    * Redistributions in binary form must reproduce the above copyright notice,
'      this list of conditions and the following disclaimer in the documentation
'      and/or other materials provided with the distribution.
'    * Neither the name of Dario Rossa nor the names of its contributors may be
'      used to endorse or promote products derived from this software without
'      specific prior written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
'AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
'ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
'(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
'LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
'ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
'SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports DotNetNuke.UI.Modules
Imports DotNetNuke.Modules.Blog.Business
Imports DotNetNuke.Services.FileSystem
Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Common
Imports BlogML.Xml
Imports System.Xml
Imports DotNetNuke.Modules.Blog.Settings
Imports DarioRossa.Modules.DnnBlogML.ExtendedMLCategoryExtensions
Imports System.Linq

''' <summary>
''' Performs import from a BlogML format file to the database.
''' </summary>
''' <remarks></remarks>
Partial Public Class Import
  Inherits ModuleUserControlBase

  Private _BlogController As New BlogController
  Const LogMessageFormat As String = "<div><img src=""{0}{1}.png"">{2}</div>"

#Region "Event Methods"

  ''' <summary>
  ''' Handles changes made to the folders dropdown, updating the file dropdown.
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  ''' <remarks></remarks>
  Protected Sub cboFolders_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cboFolders.SelectedIndexChanged
    Dim arrFiles As ArrayList = Globals.GetFileList(ModuleContext.PortalId, "xml", False, cboFolders.SelectedItem.Value)

    cboFiles.DataSource = arrFiles
    cboFiles.DataBind()
  End Sub

  ''' <summary>
  ''' Handles user's clicks on the Cancel command.
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  ''' <remarks></remarks>
  Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdCancel.Click
    Response.Redirect(NavigateURL(), True)
  End Sub

  ''' <summary>
  ''' Handles user's clicks on the Import command.
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  ''' <remarks></remarks>
  Protected Sub cmdImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdImport.Click
    ltlOutput.Text = ""

    Dim FilePath As String = ModuleContext.PortalSettings.HomeDirectoryMapPath
    FilePath &= cboFolders.SelectedItem.Value
    FilePath &= cboFiles.SelectedItem.Value
    Dim RootBlogID As Integer = CType(cboBlogs.SelectedItem.Value, Integer)

    Dim blog As BlogReader = BlogReaderFactory.GetBlogReader(GetSelectedBlogType())
    With blog
      .BlogID = RootBlogID
      .BlogMLFilePath = FilePath
      .ImportUnapprovedComments = chkUnapprovedComments.Checked
      .ImportUnapprovedTrackback = chkUnapprovedTrackbacks.Checked
      .PortalID = ModuleContext.PortalId
      .TabID = ModuleContext.TabId
    End With

    AddHandler blog.LogMessage, AddressOf Me.LogMessage

    blog.Import()

  End Sub

  ''' <summary>
  ''' Handles page load.
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="e"></param>
  ''' <remarks></remarks>
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
      Dim folders As ArrayList = DotNetNuke.Common.Utilities.FileSystemUtils.GetFoldersByUser(ModuleContext.PortalId, False, False, "READ, WRITE")

      Dim folderItems = From f As FolderInfo In folders _
                        Select New CboItem With { _
                          .ItemValue = f.FolderPath, _
                          .ItemText = If(String.IsNullOrEmpty(f.FolderPath), _
                                         Localization.GetString("Root", Me.LocalResourceFile), _
                                         f.FolderPath)}

      cboFolders.DataSource = folderItems
      cboFolders.DataBind()
      cboFolders_SelectedIndexChanged(Nothing, New System.EventArgs)

      cboBlogs.DataSource = _BlogController.ListBlogs(ModuleContext.PortalId, -1, True)
      cboBlogs.DataBind()

      ddlBlogFormat.DataSource = [Enum].GetNames(GetType(BlogType))
      ddlBlogFormat.DataBind()

    End If
  End Sub
#End Region

  Private Function GetSelectedBlogType() As BlogType
    Return CType([Enum].Parse(GetType(BlogType), ddlBlogFormat.SelectedValue), BlogType)
  End Function

  Private Sub LogMessage(ByVal sender As Object, ByVal e As LogMessageEventArgs)
    Select Case e.MessageType
      Case LogMessageType.ErrorMessage
        ltlOutput.Text &= String.Format(LogMessageFormat, ResolveUrl("~/DesktopModules/DnnBlogML/images/"), "cross", e.Message)
      Case LogMessageType.InfoMessage
        ltlOutput.Text &= String.Format(LogMessageFormat, ResolveUrl("~/DesktopModules/DnnBlogML/images/"), "information", e.Message)
      Case LogMessageType.OkMessage
        ltlOutput.Text &= String.Format(LogMessageFormat, ResolveUrl("~/DesktopModules/DnnBlogML/images/"), "tick", e.Message)
    End Select
  End Sub

  Public Class CboItem
    Public Property ItemText() As String
    Public Property ItemValue() As String
  End Class

End Class